﻿@inject DialogService DialogService
@inject RBACService RBACService

<RadzenStack Gap="1rem">
    <RadzenStack Gap="1rem">
        <RadzenStack class="rz-p-0" Orientation="Orientation.Vertical" AlignItems="AlignItems.Normal" JustifyContent="JustifyContent.Normal" Style="height: 80%">
            <RadzenCheckBoxList Data="@permissions"
                                @bind-Value=@permissionIds
                                TValue="long"
                                TextProperty="UniqueName"
                                ValueProperty="Id"
                                AllowSelectAll="true"
                                SelectAllText="全选"
                                Orientation="Orientation.Vertical">
            </RadzenCheckBoxList>
        </RadzenStack>
        <RadzenStack Gap="1rem" Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.End" Style="height: 20px;">
            <RadzenButton ButtonStyle="ButtonStyle.Secondary" Text="取消" Click="OnCancel"/>
            <RadzenButton ButtonStyle="ButtonStyle.Primary" Text="保存"  Click="OnSave" />
        </RadzenStack>
    </RadzenStack>
</RadzenStack>

@code {

    [Parameter]
    public long PRoleId { get; set; }
    
    List<Permission> permissions = default!;
    IEnumerable<long> permissionIds = default!;
    private EditContext? editContext;
    private Permission? model = default!;

    private void OnSave()
    {
        try
        {
            var input = new RolePermissionInput()
            {
               RoleId = PRoleId, 
               PermissionIds = permissionIds.ToList().AsEnumerable().ToArray()
            };
            RBACService.AssignRoleSecurity(input);
        }
        catch (Exception ex)
        {
            DialogService.Alert(ex.Message, "错误", new AlertOptions() { OkButtonText = "确定" });
        }
        DialogService.Close();
    }

    private void OnCancel()
    {
        DialogService.Close();
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        permissions = RBACService.GetAuthList(new SimpleInput(""));
        permissionIds = RBACService.GetAuthByRoleId(PRoleId);
    }

}

